import axios from 'axios'
import type { Category, BaseResponse } from '../types'

// 获取分类列表
export const getCategories = (): Promise<BaseResponse<Category[]>> => {
  return axios.get('/api/categories')
}

// 获取单个分类
export const getCategoryById = (id: number): Promise<BaseResponse<Category>> => {
  return axios.get(`/api/categories/${id}`)
}

// 创建分类
export const createCategory = (categoryData: Omit<Category, 'id' | 'createTime'>): Promise<BaseResponse<Category>> => {
  return axios.post('/api/categories', categoryData)
}

// 更新分类
export const updateCategory = (id: number, categoryData: Partial<Category>): Promise<BaseResponse<Category>> => {
  return axios.put(`/api/categories/${id}`, categoryData)
}

// 删除分类
export const deleteCategory = (id: number): Promise<BaseResponse<{ id: number }>> => {
  return axios.delete(`/api/categories/${id}`)
}

// 获取分类树结构
export const getCategoryTree = (): Promise<BaseResponse<Category[]>> => {
  return axios.get('/api/categories/tree')
}

// 获取分类下的产品数量
export const getCategoryProductCount = (id: number): Promise<BaseResponse<{ count: number }>> => {
  return axios.get(`/api/categories/${id}/product-count`)
}